package com.avic.rong.course.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.course.bean.PeiXunListBean;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.wedgit.StringUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by Chenjiaxin on 2019/8/11 18:19
 * E-Mail Address：16601203965@163.com
 */
public class PeiXunCourseVideoRvAdapter extends RecyclerView.Adapter<PeiXunCourseVideoRvAdapter.PeiXunVideoRvViewHolder>{

    private Context context;
    private ArrayList<PeiXunListBean.DataBean> list;

    public PeiXunCourseVideoRvAdapter(Context context,ArrayList<PeiXunListBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }


    public List<PeiXunListBean.DataBean> getList() {
        return list;
    }

    @NonNull
    @Override
    public PeiXunVideoRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.peixun_video_rv_item,viewGroup,false);
        return new PeiXunVideoRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PeiXunVideoRvViewHolder holder, int i) {
        Glide.with(context).applyDefaultRequestOptions(RequestOptions.bitmapTransform(new RoundedCornersTransformation(DensityUtils.dip2px(context,6),0,RoundedCornersTransformation.CornerType.ALL))).load(AppUtils.COURSE_IMAGE_URL +list.get(i).getCover()).placeholder(R.mipmap.online_default).into(holder.img);
        holder.title.setText(list.get(i).getName());
        if (TextUtils.isEmpty(list.get(i).getBuyCount()+"")){
            holder.classnum.setText("");
        }else {
            holder.classnum.setText("课时: 共"+list.get(i).getCoursesHourCount()+"课时");
        }
        if (!StringUtil.isEmpty(list.get(i).getCoursesTotleTime())){
            holder.classtime.setText("时长："+list.get(i).getCoursesTotleTime());
        }else {
            holder.classtime.setText("时长："+"暂无明细时长");
        }

        /*String timeExpend = getTimeExpend(list.get(i).getStartTime(), list.get(i).getEndTime());
        if (TextUtils.isEmpty(timeExpend)){
            holder.classtime.setText("");
        }else {
            Log.e("course", "onBindViewHolder: "+timeExpend );
            String[] split = timeExpend.split(":");
            holder.classtime.setText("时长："+list.get(i).getCoursesTotleTime());
        }*/
        holder.price.setText("¥"+list.get(i).getPrice());
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb = new StringBuffer();
                sb.append(list.get(i).getType()+"");
                sb.append(list.get(i).getId()+"");
                context.startActivity(CourseDetailActivity.getIntent(context,sb.toString(),list.get(i).getCover(),""));
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class PeiXunVideoRvViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView title,classnum,classtime,price;
        LinearLayout linearLayout;
        public PeiXunVideoRvViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.peixun_list_img);
            title = itemView.findViewById(R.id.peixun_list_title);
            classnum = itemView.findViewById(R.id.peixun_list_classnum);
            classtime = itemView.findViewById(R.id.peixun_list_classtime);
            price = itemView.findViewById(R.id.peixun_list_price);
            linearLayout = itemView.findViewById(R.id.peixun_list_LinearLayout);
        }
    }
    private String getTimeExpend(long startTime, long endTime){
        //传入字串类型 2016/06/28 08:30
        long longStart = startTime; //获取开始时间毫秒数
        long longEnd = endTime;  //获取结束时间毫秒数
        long longExpend = longEnd - longStart;  //获取时间差

        long longHours = longExpend / (60 * 60 * 1000); //根据时间差来计算小时数
        long longMinutes = (longExpend - longHours * (60 * 60 * 1000)) / (60 * 1000);   //根据时间差来计算分钟数

        return longHours + ":" + longMinutes;
    }
}
